﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename.UC
{
    public class UcTabControl:TabControl
    {
        private bool _isResizing = false;
        private const int TAB_PAGE_ITEM_HEIGHT = 40;
        public UcTabControl() 
        {
            DrawMode = TabDrawMode.OwnerDrawFixed;
            SizeMode = TabSizeMode.Fixed; // 固定模式，使ItemSize生效
            Margin = new Padding(0, 0, 0, 0);
            Padding = new Point(0, 0);

            DrawItem += OnDrawItem;
            Resize += OnResize; // 绑定Resize事件，当控件大小改变时触发
            MouseMove += OnMouseMove;

        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            Rectangle tabRect = GetTabRect(0);
            if (e.Y < tabRect.Height - 1 && e.Y > tabRect.Y + 1)
            {
                Cursor = Cursors.Hand;
            }
            else
            {
                Cursor = Cursors.Default;
            }
        }

        private void OnResize(object sender, EventArgs e)
        {
            if (DesignMode) return;  // 避免在设计时触发（可选）     
            if (_isResizing) return;    // 如果正在处理 resize，直接返回
            try
            {
                // 计算新的选项卡尺寸
                if (TabPages.Count > 0) 
                {
                    _isResizing = true; // 标记为正在处理
                    int newWidth = ClientRectangle.Width / TabPages.Count;
                    // 只有当尺寸确实变化时才更新（避免无意义的触发）               
                    if (ItemSize.Width != newWidth || ItemSize.Height != TAB_PAGE_ITEM_HEIGHT)
                    {
                        ItemSize = new Size(newWidth-1, TAB_PAGE_ITEM_HEIGHT);
                    }
                }
            }
            finally
            {
                _isResizing = false; // 处理完毕，重置标志
            }
        }
        private void OnDrawItem(object sender, DrawItemEventArgs e)
        {
            //Console.WriteLine($"OnDrawItem Tab: w:{this.Width} h:{this.Height} TabPage  " +
                //$"序号:{e.Index} 状态:{e.State.ToString().PadRight(10, '.')}\tX={e.Bounds.X}\tY={e.Bounds.Y}\tw={e.Bounds.Width}\th={e.Bounds.Height}");
            Font fntTab;//字体
            Brush bshBack;//背景颜色
            Brush bshFore;//前景颜色
            if (e.Index == SelectedIndex)    //当前Tab页的样式
            {
                fntTab = new Font(e.Font, FontStyle.Bold);
                bshBack = new SolidBrush(Color.FromArgb(58, 58, 58)); //选中的标签颜色变为红色
                bshFore = new SolidBrush(Color.White);
            }
            else    //其余Tab页的样式
            {
                fntTab = new Font(e.Font, FontStyle.Regular);
                bshBack = new SolidBrush(Color.FromArgb(242, 243, 245));
                bshFore = Brushes.Black;
            }

            ////画样式
            string tabName = TabPages[e.Index].Text;
            StringFormat sftTab = new StringFormat();
            sftTab.Alignment = StringAlignment.Center;  //水平方向居中
            sftTab.LineAlignment = StringAlignment.Center;   //垂直方向居中 
            Rectangle recTab = e.Bounds;
            e.Graphics.FillRectangle(bshBack, recTab); //填充背景区域
            recTab = new Rectangle(recTab.X, recTab.Y + 5, recTab.Width, recTab.Height - 4);
            e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);  //写入背景区域文字

        }
    }
}
